package com.wimcorp.magic.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.transaction.annotation.Transactional;

import com.wimcorp.magic.cards.PlayingCard;


@Transactional
public class PlayingCardDaoImpl implements PlayingCardDao{

	@PersistenceContext
	private EntityManager entityManager;

	public long saveOrUpdate(PlayingCard o) {
		if (o.getId() == null) {
			entityManager.persist(o);
			return o.getId();
		} else {
			return entityManager.merge(o).getId();
		}
	}

	public PlayingCard getCard(long multiverseid) {
		List<PlayingCard> cards = entityManager
				.createQuery(
						"select c from PlayingCard c where multiverseid=:multiverseid",
						PlayingCard.class).setParameter("multiverseid", multiverseid).getResultList();
		if (cards.size() > 0) {
			return cards.get(0);
		} else {
			return null;
		}

	}
	
	public PlayingCard getCardClone(long multiverseid) {
		List<PlayingCard> cards = entityManager
				.createQuery(
						"select c from PlayingCard c where multiverseid=:multiverseid",
						PlayingCard.class).setParameter("multiverseid", multiverseid).getResultList();
		if (cards.size() > 0) {
			return cards.get(0).clone();
		} else {
			return null;
		}

	}
	
}
